package com.example.fill.Test240126;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class LineView extends View {

    Paint linePaint = new Paint();
    Paint dotPaint = new Paint();
    Path path = new Path();
    private List<Commend> bean = new ArrayList<>();

    public LineView(Context context) {
        super(context);
    }

    public LineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(5f);
        linePaint.setTextSize(48);
        dotPaint.setColor(Color.GREEN);
        dotPaint.setStyle(Paint.Style.FILL);
        dotPaint.setStrokeWidth(8f);

//        long l = System.currentTimeMillis();
//        bean.addAll(Arrays.asList(
//                new Commend().setPushTime(l),
//                new Commend().setPushTime(l + 1000)
//                        .setNoBack(false)
//                        .setBackTime(l + 8000),
//                new Commend().setPushTime(l + 2000),
//                new Commend().setPushTime(l + 3000),
//                new Commend().setPushTime(l + 4000),
//                new Commend().setPushTime(l + 5000)
//                        .setNoBack(false)
//                        .setBackTime(l + 8000),
//                new Commend().setPushTime(l + 6000)
//                        .setNoBack(false)
//                        .setBackTime(l + 8000),
//                new Commend().setPushTime(l + 7000)
//                        .setNoBack(false)
//                        .setBackTime(l + 8000),
//                new Commend().setPushTime(l + 8000)
//                        .setNoBack(false)
//                        .setBackTime(l + 8000),
//                new Commend().setPushTime(l + 9000)
//                        .setNoBack(false)
//                        .setBackTime(l + 8000),
//                new Commend().setPushTime(l + 10000)
//                        .setNoBack(false)
//                        .setBackTime(l + 8000)
//        ));
    }

    public LineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public LineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    int s1 = 10;
    long first = 0;

    long end = 0;

    int p = 20;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(
                MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getMode(widthMeasureSpec)),
                MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.getMode(heightMeasureSpec))
        );

    }

    public void setBean(List<Commend> bean) {
        this.bean.clear();
        this.bean.addAll(bean);
        if (bean.isEmpty()) return;
        first = bean.get(0).getPushTime();
        end = getWidth() * 10L + first;
        invalidate();
    }

    Rect rect = new Rect();
    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(Color.WHITE);
        if (bean.isEmpty()) {
            return;
        }
        int width = getWidth();
        int height = getHeight();
        int wd2 = width / 2;
        int hd2 = height / 2;

        String sss = first +"/"+ end;
        linePaint.getTextBounds(sss,0,sss.length(),rect);
        canvas.drawText(sss,0,rect.height(),linePaint);
        canvas.drawLine(0, hd2, width, hd2, linePaint);
//        for (Commend commend : bean) {
//            if (commend.getPushTime() >= first || commend.getBackTime() <= end) {
//                long s = (commend.getPushTime() - first) * 10;
//                canvas.drawPoint(s, hd2, dotPaint);
//                if (!commend.isNoBack() && commend.getBackTime() == 0L) {
//
//                } else {
//                    long b = commend.getBackTime() - first;
//                    long wid1 = b / s1;
//                    canvas.drawPoint(wid1, hd2, dotPaint);
//
//                    path.moveTo(s, hd2);
//                    path.lineTo(s, hd2 + p);
//                    path.lineTo(wid1, hd2 + p);
//                    path.lineTo(wid1, hd2);
//
//                    canvas.drawPath(path, linePaint);
//                    path.reset();
//                    p += 20;
//                }
//            }
//        }


//        canvas.drawLine(0,hd2,width,hd2,linePaint);
//        if (bean.isEmpty()) return;
//        long pushTime = bean.get(0).getPushTime();
//
//        for (int i = 0; i < bean.size(); i++) {
//            Commend commend = bean.get(i);
//            long l = commend.getPushTime() - pushTime;
//            long wid = l / s1;
//            canvas.drawPoint(wid,hd2,dotPaint);
//            if (!commend.isNoBack() && commend.getBackTime() == 0L){
//
//            }else {
//                long b = commend.getBackTime() - pushTime;
//                long wid1 = b / s1;
//                canvas.drawPoint(wid1,hd2,dotPaint);
//
//                path.moveTo(wid,hd2);
//                path.lineTo(wid,hd2+p*i);
//                path.lineTo(wid1,hd2+p*i);
//                path.lineTo(wid1,hd2);
//
//                canvas.drawPath(path,linePaint);
//                path.reset();
//            }
//        }
    }

    float downX = 0;
    float downY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float v = event.getX() - downX;
                first = (long) (first + v);
                end = (long) (end + v);
                Log.i("TAG", "onTouchEvent: "+first +" "+end+" "+v);

                invalidate();
               break;
        }
        return true;
    }

}
